<?php
/**
 * <i>@foreach</i>组件
 * 
 * 遍历data-set
 * 
 * 可以使用的参数有：
 * - x-data-set object/array data-set变量名
 * - [x-key]  	  string 每一个元素的键
 * - x-value        mixed 每一个元素的值  
 * - [x-in]  当前节点是否在循环体内，默认为false，since 0.1  
 * - x-items 同x-data-set，@since 0.1
 * 
 * x-data-set示例：
 * - x-data-set=$array
 * - x-data-set=$array[1,3]
 * - x-data-set='json:["a","b","c","d"]'
 * - x-data-set="[1..100]"
 * 
 * 示例：
 * <pre>
 * <ul com="@foreach" x-data-set="$blog" x-key="$key" x-value="$value">
 * 	<li>{$key}: {$value}</li>
 * </ul>
 * 
 * <table border="1">
 * 	<tr com="@foreach" x-data-set="$blog" x-key="$key" x-value="$value" x-in="true">
 * 		<td>{$key}:{$value}</td>
 * 	</tr>
 * </table>
 * </pre>
 * 
 * @version $Id: IMintForeachComponent.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mint.component
 */
class IMintForeachComponent extends IAbstractMintComponent {
	/**
	 * 取得支持的参数列表
	 *
	 * @return array
	 */	
	public function getSupportedParameters() {
		return array("x-data-set", "x-key", "x-value", "x-in", "x-items");
	}
	
	/**
	 * 取得HTML属性映射
	 *
	 * @return array
	 * @since 0.0.3
	 */		
	public function getHtmlAttrMapping() {
		return array();
	}	
	
	/**
	 * 处理组件
	 *
	 * @return boolean
	 */	
	public function processComponent() {
		$parameters = $this->getParameters();
		
		$dataSetName = $this->getParameterValue("x-data-set");
		if ($dataSetName === null) {
			$dataSetName = $this->getParameterValue("x-items");
		}
		if ($dataSetName === null) {
			$this->throwVariableException("x-data-set or x-items");
		}
				
		if (isset($parameters["x-value"])) {
			$xValue = $parameters["x-value"];
			
			$dataSetVar = new IMintVariable($dataSetName);			
			$xValueVar = IMintVariable::getDefinition($parameters["x-value"]);
			if (!$dataSetVar->isValid()) {
				$this->throwVariableException("x-data-set");
			}
			if ($xValueVar == null) {
				$this->throwVariableException("x-value");
			}
			$beginText = "\n<?php\n " . $dataSetVar->getParseProcess() . " ?>";
			$dataSetVarname = $dataSetVar->getVarname();
			if (isset($parameters["x-key"])) {
				$xKeyVar = IMintVariable::getDefinition($parameters["x-key"]);
				if ($xKeyVar == null) {
					$this->throwVariableException("x-key");
				}
				$beginText .= "\n<?php ";
				$beginText .= "if (!is_array({$dataSetVarname})&&!is_object({$dataSetVarname})){\n throw new IMintException(\"parameter x-data-set '\\" . addslashes($dataSetName) . "' on @foreach component  must be an array or an object<br/>\n" .  addslashes($this->getNodeDescription()) . "\");\n}\n";
				$beginText .= "foreach (" . $dataSetVarname . " as " . $xKeyVar . "=>" . $xValueVar . ") {\n ?>\n";
			}
			else {
				$beginText .= "\n<?php ";
				$beginText .= "if (!is_array({$dataSetVarname})&&!is_object({$dataSetVarname})){\n throw new IMintException(\"parameter x-data-set '\\" . addslashes($dataSetName) . "' on @foreach component  must be an array or an object<br/>\n" .  addslashes($this->getNodeDescription()) . "\");\n}\n";
				$beginText .= "foreach (" . $dataSetVarname . " as " . $xValueVar . ") { \n ?>\n";
			}
			$endText = "\n<?php \n } \n ?>\n";
			
			//x-in
			if ($this->getParameterValue("x-in") == "true") {		
				$this->addAboveScript($beginText);
				$this->addBelowScript($endText);
			}
			else {
				$this->addAroundScript($beginText, $endText);
			}
			return true;
		}
	}
}

?>